//
//  TravelTableViewController.m
//  TripGroup
//
//  Created by apple on 15/10/24.
//  Copyright © 2015年 lanou3g.com蓝鸥科技有限公司. All rights reserved.
//

#import "TravelTableViewController.h"
#import "TravalTableViewCell.h"
#import "PKRequestManager.h"
#import "TravelWebViewController.h"
@interface TravelTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation TravelTableViewController

-(NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        self.dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = self.titleStr;
    
    [self.tableView registerClass:[TravalTableViewCell class] forCellReuseIdentifier:@"tzjj"];
    [self requestData];
    self.tableView.rowHeight = (SCREEN_HEIGHT - 64)/2 - 10;
}
// 网络请求部分
- (void)requestData
{
    [PKRequestManager PKRequestManagerrequsetWithType:GET URLString:[NSString stringWithFormat:@"http://www.57yx.com/app/%@.json",self.typeStr] ParDIc:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *dataArr = dic[@"item_list"];
        for (NSDictionary *modelDic in dataArr) {
            TravelModel *model = [[TravelModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.dataSourceArray removeLastObject];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"旅游界面数据请求失败%@",[error localizedDescription]);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TravalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tzjj" forIndexPath:indexPath];
    TravelModel *model = self.dataSourceArray[indexPath.row];
    cell.configureModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravalTableViewCell *cell = (TravalTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    TravelWebViewController *travelWebVC = [[TravelWebViewController alloc]init];
    travelWebVC.webStr = cell.configureModel.link;
    [self.navigationController pushViewController:travelWebVC animated:YES];

}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
